package com.magicsoft.share.presenter.my;

import com.magicsoft.share.base.BasePresenter;
import com.magicsoft.share.bean.my.Address;
import com.magicsoft.share.constant.APIService;
import com.magicsoft.share.constant.SPKey;
import com.magicsoft.share.rx_retrofit_network.location.retrofit.RetrofitUtil;
import com.magicsoft.share.rx_retrofit_network.location.rxandroid.LoadingTransformer;
import com.magicsoft.share.rx_retrofit_network.location.rxandroid.NetWorkTransformer;
import com.magicsoft.share.rx_retrofit_network.location.rxandroid.SimpleObserver;
import com.magicsoft.share.utils.SPUtils;
import com.magicsoft.share.view.my.IAddressView;

import java.util.List;

import io.reactivex.annotations.NonNull;

/**
 * Created by lyf on 2017/11/23/023.
 */

public class AddressPresenter extends BasePresenter<IAddressView> {
    public void addressList() {
        RetrofitUtil.getApi(APIService.class)
                .addressList(SPUtils.getInstance().getString(SPKey.TOKEN), SPUtils.getInstance().getLong(SPKey.ID))
                .compose(new NetWorkTransformer<List<Address>>())
                .compose(new LoadingTransformer<List<Address>>(mContext))
                .subscribe(new SimpleObserver<List<Address>>(mCompositeDisposable) {
                    @Override
                    public void call(@NonNull List<Address> list) {

                        getView().addressListSuccess(list);
                    }

                });
    }

    public void saveAddress(long id, int whetherDefault,
                            String appUserName, String phone,
                            String region, String addressDetail,
                            String postalCode) {
        RetrofitUtil.getApi(APIService.class)
                .saveAddress(SPUtils.getInstance().getString(SPKey.TOKEN), SPUtils.getInstance().getLong(SPKey.ID),
                        id, whetherDefault, appUserName, phone, region, addressDetail, postalCode)
                .compose(new NetWorkTransformer())
                .compose(new LoadingTransformer(mContext))
                .subscribe(new SimpleObserver(mCompositeDisposable) {


                    @Override
                    public void call(@NonNull Object o) {
                        getView().saveAddressSuccess();
                    }
                });
    }

    public void deleteAddress(long id) {
        RetrofitUtil.getApi(APIService.class)
                .deleteAddress(SPUtils.getInstance().getString(SPKey.TOKEN), id)
                .compose(new NetWorkTransformer())
                .compose(new LoadingTransformer(mContext))
                .subscribe(new SimpleObserver(mCompositeDisposable) {


                    @Override
                    public void call(@NonNull Object o) {
                        getView().saveAddressSuccess();
                    }
                });
    }


}
